package com.quzi.kettle.activitise.added;

import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.util.Log;

public class ActivityManager {
	private String Tag = this.getClass().getSimpleName();
	private static List<Activity> activityList = new LinkedList<Activity>();
	private static ActivityManager instance;

	private ActivityManager() {
	}

	// 单例模式中获取唯�?的MyApplication实例
	public static ActivityManager getInstance() {
		if (null == instance) {
			instance = new ActivityManager();
		}
		return instance;
	}

	// 添加Activity到容器中
	public void addActivity(Activity activity) {
		Log.e(Tag, "add Activity   " + activity.getClass().getSimpleName());
		activityList.add(activity);
	}

	public void removeActivity(Activity activity) {
		Log.e(Tag, "remove Activity   " + activity.getClass().getSimpleName());
		activityList.remove(activity);
	}

	// 遍历�?有Activity并finish
	public void exit() {
		for (Activity activity : activityList) {
			if (!activity.isFinishing()) {
				activity.finish();
			}
		}
		System.exit(0);
	}

	public void exitNumActivity(int num) {
		for (int i = 0; i < num; i++) {
			activityList.get(activityList.size() - 1).finish();
			activityList.remove(activityList.size() - 1);
		}

	}

	public void exitBefore() {
		Log.e(Tag, "exitBefore" + "   " + ActivityList());
		for (int i = 0; i < activityList.size() - 1; i++) {
			Activity activity = activityList.get(i);
			if (!activity.isFinishing()) {
				activity.finish();
			}
		}
	}

	private String ActivityList() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < activityList.size(); i++) {
			sb.append(activityList.get(i).getClass().getSimpleName());
			sb.append("     ");
		}
		return sb.toString();
	}
}
